/**
 * 棋盘逻辑js
 */
import common from '../global/common'
import BWU from '../global/bw-util';

cc.Class({
    extends: cc.Component,

    properties: {
        closeBtn: cc.Button,//关闭按钮
        board: cc.Node,//棋盘
        pieces: cc.Prefab,//棋子
        touchPoint: cc.Prefab,//触摸点
        imageFrame: [cc.SpriteFrame],//帅将图集
        playerIamge: cc.Sprite,//玩家图片
        btns: [cc.Button],//按钮
    },
    init(option) {

    },


    onLoad() {
        this.closeBtn.node.on('click', this.close.bind(this));
        this.pointHeight = 68;//格子的间隔
        this.pointWidth = 68;
        this.pointMinX = 1;//x坐标最小值
        this.pointMaxX = 9;//x坐标最大值
        this.pointMinY = 1;//y坐标最小值
        this.pointMaxY = 10;//y坐标最大值
        const PT = common.PieceType;

        let red = [
            { name: '卒', type: PT.BIN, pos: '1,4', id: 1 },
            { name: '卒', type: PT.BIN, pos: '3,4', id: 1 },
            { name: '卒', type: PT.BIN, pos: '5,4', id: 1 },
            { name: '卒', type: PT.BIN, pos: '7,4', id: 1 },
            { name: '卒', type: PT.BIN, pos: '9,4', id: 1 },
            { name: '炮', type: PT.PAO, pos: '2,3', id: 2 },
            { name: '炮', type: PT.PAO, pos: '8,3', id: 2 },
            { name: '车', type: PT.JU, pos: '1,1', id: 2 },
            { name: '车', type: PT.JU, pos: '9,1', id: 2 },
            { name: '马', type: PT.MA, pos: '2,1', id: 2 },
            { name: '马', type: PT.MA, pos: '8,1', id: 2 },
            { name: '相', type: PT.XIANG, pos: '3,1', id: 2 },
            { name: '相', type: PT.XIANG, pos: '7,1', id: 2 },
            { name: '士', type: PT.SHI, pos: '4,1', id: 2 },
            { name: '士', type: PT.SHI, pos: '6,1', id: 2 },
            { name: '帅', type: PT.SHUAI, pos: '5,1', id: 2 },
        ]
        for (let i = 0; i < red.length; i++) {
            let item = red[i];
            let node = cc.instantiate(this.pieces);
            let pos = item.pos.split(',');
            let x = (pos[0] - 1) * this.pointWidth;
            let y = (pos[1] - 1) * this.pointHeight;
            node.x = x;
            node.y = y;
            item.playerType = common.PlayerType.RED;
            node.getComponent('pieces').init(item);
            this.board.addChild(node);
        }
        let black = [
            { name: '兵', type: PT.BIN, pos: '1,7', id: 1 },
            { name: '兵', type: PT.BIN, pos: '3,7', id: 1 },
            { name: '兵', type: PT.BIN, pos: '5,7', id: 1 },
            { name: '兵', type: PT.BIN, pos: '7,7', id: 1 },
            { name: '兵', type: PT.BIN, pos: '9,7', id: 1 },
            { name: '炮', type: PT.PAO, pos: '2,8', id: 2 },
            { name: '炮', type: PT.PAO, pos: '8,8', id: 2 },
            { name: '车', type: PT.JU, pos: '1,10', id: 2 },
            { name: '车', type: PT.JU, pos: '9,10', id: 2 },
            { name: '马', type: PT.MA, pos: '2,10', id: 2 },
            { name: '马', type: PT.MA, pos: '8,10', id: 2 },
            { name: '象', type: PT.XIANG, pos: '3,10', id: 2 },
            { name: '象', type: PT.XIANG, pos: '7,10', id: 2 },
            { name: '士', type: PT.SHI, pos: '4,10', id: 2 },
            { name: '士', type: PT.SHI, pos: '6,10', id: 2 },
            { name: '将', type: PT.SHUAI, pos: '5,10', id: 2 },
        ]

        for (let i = 0; i < black.length; i++) {
            let item = black[i];
            let node = cc.instantiate(this.pieces);
            let pos = item.pos.split(',');
            let x = (pos[0] - 1) * this.pointWidth;
            let y = (pos[1] - 1) * this.pointHeight;
            node.x = x;
            node.y = y;
            item.playerType = common.PlayerType.BLACK;
            node.getComponent('pieces').init(item);
            this.board.addChild(node);
        }

    },
    createPieces() {

    },
    start() {

    },
    close() {
        this.destroyNode();
    },
    destroyNode() {
        this.node.removeFromParent();
        this.node.destroy();
    },
    onDestroy() {
        BWU.toReleasePrefab(common.prefabName.board);
    },
    // update (dt) {},
});
